<?php
$this->headTitle('Please Log In');
?>

<script type="text/javascript">
function authenticateUser() {
	$('#authWaiting').dialog('open');
	$.ajax({
		data:		{
			username:	$('#username').val(),
			password:	$('#password').val()
		},
		dataType:	'json',
		error:		function(rq, status, error) {
			alert('There was a problem searching for the user: ' + error);
		},
		success:	function(rsp, status) {
			if(rsp.status == 1) {
				window.location = "<?php echo $this->baseUrl(); ?>/";
			} else {
				$('#authWaiting').dialog('close');
				$('#authMessage').html('Error - ' + rsp.message);
			}
		},
		type:		'POST',
		url:		'<?php echo $this->baseUrl(); ?>/auth/index/process/'
	});
}

$(document).ready(function() {
	$('#login').click(function() {
		authenticateUser();
		return false;
	});

	$('#authWaiting').dialog({
		bgiframe:	true,
		autoOpen:	false,
		modal:		true,
		width:		400
	});

	$('#registerConf').dialog({
		bgiframe:	true,
		autoOpen:	false,
		modal:		true,
		width:		400,
		buttons:	{
			'Register User' : function() {
				$.ajax({
					data:		{
						email:	$('#email').val(),
						conf:	$('#conf').val()
					},
					dataType:	'json',
					error:		function(rq, status, error) {
						alert('There was a looking up the registration: ' + error);
					},
					success:	function(rsp, status) {
						if(rsp.status == 1) {
							window.location = "<?php echo $this->baseUrl(); ?>/auth/newuser/";
						} else {
							$('#registerConf').dialog('close');
							$('#email').val("");
							$('#conf').val("");
							$('#authMessage').html('Error - ' + rsp.message);
						}
					},
					type:		'POST',
					url:		'<?php echo $this->baseUrl(); ?>/auth/newuser/confirm/'
				});
			}
		}
	});

	$('#registerNewUser').click(function() {
		$('#registerConf').dialog('open');
	});
});
</script>

<h1>User Authentication</h1>

<div id="authMessage">
	<?php echo $this->message; ?>
</div>


<div style="width: 200px; margin-left: auto; margin-right: auto;" class="panel">
	<form id="loginForm" method="POST" action="javascript:return false;">
	<dl class="form">
		<dt>Username:</dt>
			<dd><input type="text" id="username" name="username" style="width: 200px"></dd>
		<dt>Password:</dt>
			<dd><input type="password" id="password" name="password" style="width: 200px"></dd>
	</dl>
	<input type="submit" id="login" value="Log In" style="float: right; clear: both">
	</form>

	<p>
	<a href="#" id="registerNewUser">Register New User</a>
</div>

<div id="authWaiting" title="Working...">
	Please wait while we log you in...

	<p>
	<img src="<?php echo $this->baseUrl(); ?>/images/ajax-loader.gif">
</div>

<div id="registerConf" title="User Information">
	Please enter your e-mail address and confirmation code

	<dl class="form">
		<dt>E-mail Address:</dt>
			<dd><input type="text" id="email" name="email" style="width: 200px"></dd>
		<dt>Confirmation:</dt>
			<dd><input type="text" id="conf" name="conf" style="width: 200px"></dd>
	</dl>
</div>